rm(list = ls(all.names = TRUE))
library(foreign)
library(arm)
library(readstata13)
#detach(package:readstata13)

# library(plyr)
Data <- read.dta13("~/Dropbox/Mingling for IO/ICC paper data/IO Replication PUTNAM/Layering_Data_Oct_2019 replication.dta")



Data <- subset(Data, year == 2006)
setwd("~/Dropbox/Mingling for IO/ICC paper data/")

#head(Data)
#sort(names(Data))


#stuff looking at subset of data to try to figure out missing graph 7/20/19
third_figure_data = subset(Data, earlyrat == 1 & gspstatus == 0 &  year == 2006)
third_figure_data$exposure4
third_figure_data$percexport_1yrlag
third_figure_data$aiddollarspc

length(third_figure_data$exposure4)
length(third_figure_data$percexport_1yrlag)
length(third_figure_data$aiddollarspc)

#GRAPHS FOR FINAL IO R&R

pdf("exports_defpact_quartet.pdf")
par(mfrow  = c(2,2))
par(mar = c(3.5,5,2.8,.3))


y.vec <- seq(-3,7, by  = 2)


with(subset(Data, earlyrat == 1 & defpact == 1),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  #axis(1, at)
  mtext("exports to u.s. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("EARLY ICC", side =3, cex = 1.4, padj = -1)
  mtext("DEFENSE PACT", side =2, cex = 1.4, padj = -3.5)
})



with(subset(Data, earlyrat == 0 & defpact == 1),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("exports to u.s. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("LATE/NON ICC", side =3, cex = 1.4, padj = -1)
  
})

with(subset(Data, earlyrat == 1 & defpact == 0),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("exports to u.s. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("NO PACT", side =2, cex = 1.4, padj = -3.5)
  
})

with(subset(Data, earlyrat == 0 & defpact == 0),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("exports to u.s. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
})
dev.off()

pdf("exports_gsp_quartet.pdf")

par(mfrow  = c(2,2))
par(mar = c(3.5,5,2.8,.3))


with(subset(Data, earlyrat == 1 & gspstatus == 1),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  #axis(1, at)
  mtext("exports to u.s. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("EARLY ICC", side =3, cex = 1.4, padj = -1)
  mtext("GSP", side =2, cex = 1.4, padj = -3.5)
})

with(subset(Data, earlyrat == 0 & gspstatus == 1),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("exports to U.S. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("LATE/NON ICC", side =3, cex = 1.4, padj = -1)
  
})

with(subset(Data, earlyrat == 1 & gspstatus == 0),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("exports to U.S. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("NO GSP", side =2, cex = 1.4, padj = -3.5)
  
})

with(subset(Data, earlyrat == 0 & gspstatus == 0),{
  scatter.smooth( percexport_1yrlag,exposure4, ylim = c(-3,7), xlim = c(0,1), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ percexport_1yrlag), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("exports to U.S. (%)", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
})
dev.off()

#To check range of data dropping NAs:  range(x, na.rm = TRUE)
with(subset(Data), range(percexport_1yrlag, na.rm = T))
# 

pdf("aid_defpact_quartet.pdf")
par(mfrow  = c(2,2))
par(mar = c(3.5,5,2.8,.3))


with(subset(Data, earlyrat == 1 & defpact == 1),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black", pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  #axis(1, at)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("EARLY ICC", side =3, cex = 1.4, padj = -1)
  mtext("DEFENSE PACT", side =2, cex = 1.4, padj = -3.5)
})

with(subset(Data, earlyrat == 0 & defpact == 1),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("LATE/NON ICC", side =3, cex = 1.4, padj = -1)
  
})

with(subset(Data, earlyrat == 1 & defpact == 0),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("NO PACT", side =2, cex = 1.4, padj = -3.5)
  
})

with(subset(Data, earlyrat == 0 & defpact == 0),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n")
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
})
dev.off()

#To check range of data dropping NAs, range(x, na.rm = TRUE)
with(subset(Data), range (log(aiddollarspc + 1), na.rm = T))

#JRL:  to re-create the problem you can see the below - span 1 induces the problem
# foo.good  <- subset(Data, earlyrat == 0 & gspstatus == 0,  select  =c("aiddollarspc", "exposure4"))
# foo.bad   <- subset(Data, earlyrat == 1 & gspstatus == 0,  select  =c("aiddollarspc", "exposure4"))
# #JRL: note these have NAs... is that ok?
# scatter.smooth(log(foo.bad$aiddollarspc + 1), foo.bad$exposure4, span = 1)
# scatter.smooth(log(foo.bad$aiddollarspc + 1), foo.bad$exposure4,  span =2/3)

pdf("aid_gsp_quartet.pdf")
par(mfrow  = c(2,2))
par(mar = c(3.5,5,2.8,.3))

with(subset(Data, earlyrat == 1 & gspstatus == 1),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n", span = .9)
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  #axis(1, at)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("EARLY ICC", side =3, cex = 1.4, padj = -1)
  mtext("GSP", side =2, cex = 1.4, padj = -3.5)
})

with(subset(Data, earlyrat == 0 & gspstatus == 1),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n", span = .9)
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("LATE/NON ICC", side =3, cex = 1.4, padj = -1)
  
})
#to check range on aid data
with(subset(Data), range (log(aiddollarspc + 1), na.rm = T))

#this below was the broken one... but i changed span to 2/3

#TLP -- this is still the problem child...

with(subset(Data, earlyrat == 1 & gspstatus == 0),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n", span = .92)
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
  mtext("NO GSP", side =2, cex = 1.4, padj = -3.5)
  
})
#to check range on aid data
with(subset(Data), range (log(aiddollarspc + 1), na.rm = T))

with(subset(Data, earlyrat == 0 & gspstatus == 0),{
  scatter.smooth( log(aiddollarspc + 1),exposure4, ylim = c(-3,7), xlim = c(0,5.2), type = "p", col = "black",pch = 1, cex  = .6, axes = TRUE, xlab = "", ylab = "", yaxt  = "n", span = .92)
  abline(lm(exposure4 ~ log(aiddollarspc + 1)), col = "darkgrey", lty = 2)
  axis(2, at=y.vec)
  mtext("u.s. aid per capita", side = 1, padj = 3)
  mtext("assurance", side = 2, padj = -2.7)
})

#to check range on aid data
with(subset(Data), range (log(aiddollarspc + 1), na.rm = T))
dev.off()



